//
//  LBWStatusRetweetView.swift
//  weibo
//
//  Created by Limonwei on 15/12/9.
//  Copyright © 2015年 heima. All rights reserved.
//

//转发微博部分

import UIKit
import SnapKit

class LBWStatusRetweetView: UIView {
    
    //当前视图的底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: LBWStatusViewModel? {
        
        //当外界设置这个属性的时候,会调用didSet方法 ,在此处填充控件的内容
        didSet{
            //内容
            //contentLabel.text = statusViewModel?.retweetText
            contentLabel.attributedText = statusViewModel?.retweetedStatusAttr
            contentLabel.linkResults = statusViewModel?.retweetedLinkResults
            //判断是否有图
            //取出旧的约束
            self.bottomConstraint?.uninstall()
            
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
                //设置数据
                pictureView.pic_urls = pic_urls
                
                //显示
                pictureView.hidden = false
                
                //更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(LBWStatusCellMargin).constraint
                })
                
            }else{
                
                pictureView.hidden = true
                
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(LBWStatusCellMargin).constraint
                })
                
            }
            
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        //背景颜色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(LBWStatusCellMargin)
            make.top.equalTo(self).offset(LBWStatusCellMargin)
            make.trailing.equalTo(self).offset(-LBWStatusCellMargin)
        }
        
        //配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(LBWStatusCellMargin)
        }
        
        //当前控件的高与内容的高度一样的
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint = make.bottom.equalTo(pictureView).offset(LBWStatusCellMargin).constraint
        }
        
        
    }
    
    //懒加载控件  转发的微博内容
    private lazy var contentLabel: LBWStatusLabel = LBWStatusLabel(textColor: UIColor.darkGrayColor(), fontSize: LBWContentFont, maxWidth: SCREENW - 2 * LBWStatusCellMargin)
    
    //转发微博的配图
    private lazy var pictureView: LBWStatusPictureView = {
       
        let view = LBWStatusPictureView()
        
        view.backgroundColor = self.backgroundColor
        
        return view
        
    }()

}



















